// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_DELEGATE_H__
#define __INCLUDED_CBL_DELEGATE_H__

CB_NAMESPACE_START

// -------------------------------------------------------------------------------------------------
//	CDelegate
// 	Delegate implementation without parameter support
// -------------------------------------------------------------------------------------------------

class CDelegate
{
public:
    
    CDelegate() : m_pObject(NULL) {}
    
private:

    CDelegate(void* _pObject) : m_pObject(_pObject) {}

public:
    
    template <typename TClass, void (TClass::*TMethod)()>
    static CB_FORCEINLINE CDelegate CB_INLINE_ATTR Create(TClass* _pObject);
    
public:
    
    CB_FORCEINLINE void CB_INLINE_ATTR operator () ();
    
private:
    
	void* m_pObject;
    void (*m_pFunctionPointer)(void*);

	template <typename TClass, void (TClass::*TMethod)()>
	static CB_FORCEINLINE void CB_INLINE_ATTR ObjectFunction(void* _pObject);
};

// -------------------------------------------------------------------------------------------------
//	CInOutDelegate
// 	Delegate implementation with parameter/return value support
// -------------------------------------------------------------------------------------------------

class CInOutDelegate
{
public:
    
    CInOutDelegate() : m_pObject(NULL) {}
    
private:

    CInOutDelegate(void* _pObject) : m_pObject(_pObject) {}

public:
    
    template <typename TClass, void (TClass::*TMethod)(void*)>
    static CB_FORCEINLINE CInOutDelegate CB_INLINE_ATTR Create(TClass* _pObject);
    
public:
    
    CB_FORCEINLINE void CB_INLINE_ATTR operator () (void* _pInOut);
    
private:
    
	void* m_pObject;
    void (*m_pFunctionPointer)(void*, void*);

	template <typename TClass, void (TClass::*TMethod)(void*)>
	static CB_FORCEINLINE void CB_INLINE_ATTR ObjectFunction(void* _pObject, void* _pInOut);
};

#include "CBL_Delegate.hpp"

// -------------------------------------------------------------------------------------------------
//	CConstDelegate
// 	Delegate implementation without parameter support for const objects
// -------------------------------------------------------------------------------------------------

class CConstDelegate
{
public:
    
    CConstDelegate() : m_pObject(NULL) {}
    
private:

    CConstDelegate(const void* _pObject) : m_pObject(_pObject) {}

public:
    
    template <typename TClass, void (TClass::*TMethod)() const>
    static CB_FORCEINLINE CConstDelegate CB_INLINE_ATTR Create(const TClass* _pObject);
    
public:
    
    CB_FORCEINLINE void CB_INLINE_ATTR operator () ();
    
private:
    
	const void* m_pObject;
    void (*m_pFunctionPointer)(const void*);

	template <typename TClass, void (TClass::*TMethod)() const>
	static CB_FORCEINLINE void CB_INLINE_ATTR ObjectFunction(const void* _pObject);
};

// -------------------------------------------------------------------------------------------------
//	CInOutConstDelegate
// 	ConstDelegate implementation with parameter/return value support
// -------------------------------------------------------------------------------------------------

class CInOutConstDelegate
{
public:
    
    CInOutConstDelegate() : m_pObject(NULL) {}
    
private:

    CInOutConstDelegate(const void* _pObject) : m_pObject(_pObject) {}

public:
    
    template <typename TClass, void (TClass::*TMethod)(void*) const>
    static CB_FORCEINLINE CInOutConstDelegate CB_INLINE_ATTR Create(const TClass* _pObject);
    
public:
    
    CB_FORCEINLINE void CB_INLINE_ATTR operator () (void* _pInOut);
    
private:
    
	const void* m_pObject;
    void (*m_pFunctionPointer)(const void*, void*);

	template <typename TClass, void (TClass::*TMethod)(void*) const>
	static CB_FORCEINLINE void CB_INLINE_ATTR ObjectFunction(const void* _pObject, void* _pInOut);
};

#include "CBL_ConstDelegate.hpp"

CB_NAMESPACE_END

#endif